﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Default : System.Web.UI.Page
{
    LocalFunctions FUNC;
    int iPageID = 1;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Admin)this.Master).FUNC;

        hlImages.Text = FUNC.GetPhrase(4);
        lblPageName.Text = FUNC.GetPhrase(6);
        lblKeywords.Text = FUNC.GetPhrase(7);
        lblDescription.Text = FUNC.GetPhrase(8);

        btnArticleSave.Text = FUNC.GetPhrase(9);

        lblTextTitle.Text = FUNC.GetPhrase(10);
        lblTextContent.Text = FUNC.GetPhrase(11);

        lblLeftUpload.Text = FUNC.GetPhrase(19);
        lblLeftTitle.Text = FUNC.GetPhrase(10);
        lblLeftText.Text = FUNC.GetPhrase(11);

        lblCenterUpload.Text = FUNC.GetPhrase(19);
        lblCenterTItle.Text = FUNC.GetPhrase(10);
        lblCenterText.Text = FUNC.GetPhrase(11);

        lblRightUpload.Text = FUNC.GetPhrase(19);
        lblRightTitle.Text = FUNC.GetPhrase(10);
        lblRightText.Text = FUNC.GetPhrase(11);

        btnSaveThree.Text = FUNC.GetPhrase(9);

        // Get data from DB about page
        if (!IsPostBack)
        {
            GetParameters();
            GetPageData();
       
            hpNewsBlock.Visible = true;
            Dal.arParams.Clear();
            Dal.arParams.Add(FUNC.LangID); // Curent language
            DataTable DT = Dal.ExecuteDBsp("Admin_GetHPNewsBlock", Dal.arParams);
            if(DT!=null)
                if (DT.Rows.Count > 0)
                {
                    DataRow Row = DT.Rows[0];
                    hfImgLeft.Value = Row["Img"].ToString();
                    hfBlckLeft.Value = Row["ID"].ToString();
                    txtTTLLeft.Text = Row["Title"].ToString();
                    txtBlockLeft.Text = Helper.ConvertStringFromSql(Row["Content"].ToString());
                    imgLeft.ImageUrl = "~/content/galery/" + hfImgLeft.Value;

                    Row = DT.Rows[1];
                    hfCtrlBlck.Value = Row["ID"].ToString();
                    hfImgCenter.Value = Row["Img"].ToString();
                    txtTTLCenter.Text = Row["Title"].ToString();
                    txtBlockCenter.Text = Helper.ConvertStringFromSql(Row["Content"].ToString());
                    imgCenter.ImageUrl = "~/content/galery/" + hfImgCenter.Value;

                    Row = DT.Rows[2];
                    hfRtBlck.Value = Row["ID"].ToString();
                    hfImgRight.Value = Row["Img"].ToString();
                    txtTTLRight.Text = Row["Title"].ToString();
                    txtBlockRight.Text = Helper.ConvertStringFromSql(Row["Content"].ToString());
                    imgRight.ImageUrl = "~/content/galery/" + hfImgRight.Value;
                }
        }
    }

    private void GetParameters()
    {
        //hfID.Value = Request.QueryString["ID"];
        
        //Int32.TryParse(hfID.Value, out iPageID);
    }

    private void GetPageData()
    {
        
        if (iPageID > 0)
        {
            Dal.arParams.Clear();
            Dal.arParams.Add(iPageID); // typeID - 1- mainPage
            DataTable DT = Dal.ExecuteDBsp("Admin_GetPageByID", Dal.arParams);

            // If not null - fill fields
            if (DT != null)
                if (DT.Rows.Count > 0)
                {
                    DataRow Row = DT.Rows[0];
                    hfID.Value = Row["ID"].ToString();

                    txtPageTitle.Text = Row["PageTitle"].ToString();
                    txtKeywords.Text = Row["Keywords"].ToString();
                    txtDescription.Text = Row["Description"].ToString();

                    txtMainBlockTitle.Text = Row["Title"].ToString();
                    txtContent.Text = Helper.ConvertStringFromSql(Row["Content"].ToString());
                }
        }
    }

    protected void btnArticleSave_Click(object sender, EventArgs e)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(Convert.ToInt32(hfID.Value)); //Page ID

        Dal.arParams.Add(txtPageTitle.Text);
        Dal.arParams.Add(txtKeywords.Text);
        Dal.arParams.Add(txtDescription.Text);

        Dal.arParams.Add(txtMainBlockTitle.Text); //Title
        Dal.arParams.Add(Helper.ConvertStringToSql(txtContent.Text)); //Content
        Dal.ExecuteDBsp("Admin_UpdatePageByID", Dal.arParams,true);
    }

    protected void btnThreeBlocksSave_Click(object sender, EventArgs e)
    {
        // Save 1st Block
        String sLeftImg = hfImgLeft.Value;
        if (uplLeft.HasFile)
        {
            string sExt = System.IO.Path.GetExtension(uplLeft.PostedFile.FileName);
            sLeftImg="hp_" + Helper.GenerateUniqueFileName() + sExt;
            uplLeft.SaveAs(LocalFunctions.sMappedPath + "\\" + sLeftImg);
            imgLeft.ImageUrl = "~/content/galery/" + sLeftImg;
            hfImgLeft.Value = sLeftImg;
        }

        Dal.arParams.Clear();
        Dal.arParams.Add(Convert.ToInt32(hfBlckLeft.Value)); // ID
        Dal.arParams.Add(sLeftImg); //Image
        Dal.arParams.Add(txtTTLLeft.Text); //Title
        Dal.arParams.Add(Helper.ConvertStringToSql(txtBlockLeft.Text)); //Content
        Dal.ExecuteDBsp("Admin_UpdateHPNewsBlock", Dal.arParams, true);

        // Save 2st Block
        String sCenterImg = hfImgCenter.Value;
        if (uplCenter.HasFile)
        {
            string sExt = System.IO.Path.GetExtension(uplCenter.PostedFile.FileName);
            sCenterImg="hp_"+Helper.GenerateUniqueFileName() + sExt;
            uplCenter.SaveAs(LocalFunctions.sMappedPath + "\\" + sCenterImg);
            imgCenter.ImageUrl = "~/content/galery/" + sCenterImg;
            hfImgCenter.Value = sCenterImg;
        }
        Dal.arParams.Clear();
        Dal.arParams.Add(Convert.ToInt32(hfCtrlBlck.Value)); // ID
        Dal.arParams.Add(sCenterImg); //Image
        Dal.arParams.Add(txtTTLCenter.Text); //Title
        Dal.arParams.Add(Helper.ConvertStringToSql(txtBlockCenter.Text)); //Content
        Dal.ExecuteDBsp("Admin_UpdateHPNewsBlock", Dal.arParams, true);

        // Save 1st Block
        String sRightImg = hfImgRight.Value;
        if (uplRight.HasFile)
        {
            string sExt = System.IO.Path.GetExtension(uplRight.PostedFile.FileName);
            sRightImg="hp_" + Helper.GenerateUniqueFileName() +sExt;
            uplRight.SaveAs(LocalFunctions.sMappedPath + "\\" + sRightImg);
            imgRight.ImageUrl = "~/content/galery/" + sRightImg;
            hfImgRight.Value = sRightImg;
        }
        Dal.arParams.Clear();
        Dal.arParams.Add(Convert.ToInt32(hfRtBlck.Value)); // ID
        Dal.arParams.Add(sRightImg); //Image
        Dal.arParams.Add(txtTTLRight.Text); //Title
        Dal.arParams.Add(Helper.ConvertStringToSql(txtBlockRight.Text)); //Content
        Dal.ExecuteDBsp("Admin_UpdateHPNewsBlock", Dal.arParams, true);
    }
}